
package com.dps.omc.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.*;
import com.dps.omc.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class UserBizRightsAction extends AbstractAdminController<IUserService>{

    @Autowired
    private IUserBizRightsService userBizRightsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserBizTypeService userBizTypeService;
    @Autowired
    private IUserBizUnitService userBizUnitService;
    @Autowired
    private IBizTypeService bizTypeService;
    @Autowired
    private IBizUnitService bizUnitService;

    @RequestMapping(value = "/userbizrights/list")
    public String list(User user, ModelMap model, PageParam pageParam,
                       HttpServletRequest request, HttpServletResponse response) {

        // 获取参数
        Map<String, Object> conditions = getQueryMap(user);
        BizData4Page<User> pageCtx = doPage(model, conditions, pageParam);

        model.addAttribute("user", user);
        return "/module/userbizrights/list";
    }

    @RequestMapping(value = "/userbizrights/edit")
    public String edit(String id, ModelMap model, HttpServletRequest request,
                       HttpServletResponse response) {

        User user = userService.fetch(id);

        List<BizType> bizTypes = bizTypeService.findAll();
        List<BizUnit> bizUnits = bizUnitService.findAll();

        model.addAttribute("bizTypes", bizTypes);
        model.addAttribute("bizUnits", bizUnits);
        List<UserBizType> userBizTypes = userBizTypeService.findList("userId",user.getId());
        List<UserBizUnit> userBizUnits = userBizUnitService.findList("userId",user.getId());

        model.addAttribute("user", user);
//        model.addAttribute("userBizTypes", userBizTypes);
//        model.addAttribute("userBizUnits", userBizUnits);
        model.addAttribute("userBizTypeMap", this.getUserBizTypeMap(userBizTypes));
        model.addAttribute("userBizUnitMap", this.getUserBizUnitMap(userBizUnits));
        return "module/userbizrights/edit";
    }

    private Map<String,String> getUserBizTypeMap(List<UserBizType> userBizTypes)
    {
        Map<String,String> map = new HashMap<>();
        for(UserBizType userBizType:userBizTypes)
        {
            map.put(userBizType.getBizType(),userBizType.getBizType());
        }
        return map;
    }

    private Map<String,String> getUserBizUnitMap(List<UserBizUnit> userBizUnits)
    {
        Map<String,String> map = new HashMap<>();
        for(UserBizUnit userBizUnit:userBizUnits)
        {
            map.put(userBizUnit.getBizUnit(),userBizUnit.getBizUnit());
        }
        return map;
    }

    @RequestMapping(value = "/userbizrights/save")
    @ResponseBody
    public String save(HttpServletRequest request) {
        User creater = getCurrentUser();
        String[] bizUnitArray = request.getParameterValues("bizUnitArray");
        String[] bizTypeArray = request.getParameterValues("bizTypeArray");
        String userId = request.getParameter("userId");
        User user = userService.fetch(userId);

        List<BizUnit> bizUnits = new ArrayList<>();
        List<BizType> bizTypes = new ArrayList<>();

        userBizUnitService.deleteByProperty("userId",userId);
        for(String bizUnitStr:bizUnitArray)
        {
            if(StringUtils.isNotBlank(bizUnitStr))
            {
                bizUnits.add(this.getBizUnit(bizUnitStr));
            }
        }

        userBizTypeService.deleteByProperty("userId",userId);
        for(String bizTypeStr:bizTypeArray)
        {
            if(StringUtils.isNotBlank(bizTypeStr))
            {
                bizTypes.add(this.getBizType(bizTypeStr));
            }
        }
        for(BizUnit bizUnit:bizUnits)
        {
            UserBizUnit userBizUnit = new UserBizUnit();
            userBizUnit.setId(IdGenerator.createNewId());
            userBizUnit.setUserId(user.getId());
            userBizUnit.setUserName(user.getUserName());
            userBizUnit.setRealName(user.getRealName());
            userBizUnit.setBizUnitId(bizUnit.getId());
            userBizUnit.setBizUnit(bizUnit.getUnitName());
            userBizUnit.setBizUnitName(bizUnit.getNote());
            userBizUnitService.add(userBizUnit);
        }

        for(BizType bizType:bizTypes)
        {
            UserBizType userBizType = new UserBizType();
            userBizType.setId(IdGenerator.createNewId());
            userBizType.setUserId(user.getId());
            userBizType.setUserName(user.getUserName());
            userBizType.setRealName(user.getRealName());
            userBizType.setBizTypeId(bizType.getId());
            userBizType.setBizType(bizType.getTypeName());
            userBizType.setBizTypeName(bizType.getNote());
            userBizTypeService.add(userBizType);
        }

//        for(String bizUnitStr:bizUnitArray)
//        {
//            if(StringUtils.isNotBlank(bizUnitStr))
//            {
////                String[] bizUnitArr = bizUnitStr.split("#");
//                BizUnit bizUnit = this.getBizUnit(bizUnitStr);
//                UserBizUnit userBizUnit = new UserBizUnit();
//                userBizUnit.setId(IdGenerator.createNewId());
//                userBizUnit.setUserId(user.getId());
//                userBizUnit.setUserName(user.getUserName());
//                userBizUnit.setRealName(user.getRealName());
//                userBizUnit.setBizUnitId(bizUnit.getId());
//                userBizUnit.setBizUnit(bizUnit.getUnitName());
//                userBizUnit.setBizUnitName(bizUnit.getNote());
//                userBizUnitService.add(userBizUnit);
//            }
//        }
//
//        for(String bizTypeStr:bizTypeArray)
//        {
//            if(StringUtils.isNotBlank(bizTypeStr))
//            {
////                String[] bizTypeArr = bizTypeStr.split("#");
//                BizType bizType = this.getBizType(bizTypeStr);
//                UserBizType userBizType = new UserBizType();
//                userBizType.setId(IdGenerator.createNewId());
//                userBizType.setUserId(user.getId());
//                userBizType.setUserName(user.getUserName());
//                userBizType.setRealName(user.getRealName());
//                userBizType.setBizTypeId(bizType.getId());
//                userBizType.setBizType(bizType.getTypeName());
//                userBizType.setBizTypeName(bizType.getNote());
//                userBizTypeService.add(userBizType);
//            }
//        }
        Boolean mailAble = true;
        Boolean dbaFlag = false;

        userBizRightsService.create(user,bizUnits,bizTypes,mailAble,dbaFlag,creater);
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }

    private BizUnit getBizUnit(String bizUnitStr)
    {
        String[] bizUnitArr = bizUnitStr.split("#");
        BizUnit bizUnit = new BizUnit();
        bizUnit.setId(bizUnitArr[0]);
        bizUnit.setUnitName(bizUnitArr[1]);
        bizUnit.setNote(bizUnitArr[2]);
        return bizUnit;
    }

    private BizType getBizType(String bizTypeStr)
    {
        String[] bizTypeArr = bizTypeStr.split("#");
        BizType bizType = new BizType();
        bizType.setId(bizTypeArr[0]);
        bizType.setTypeName(bizTypeArr[1]);
        bizType.setNote(bizTypeArr[2]);
        return bizType;
    }
    
    @RequestMapping(value="/userbizrights/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("userBizRights", new UserBizRights());
        return "module/userbizrights/edit";
    }    
    
//    @RequestMapping(value="/userbizrights/edit")
//    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
//
//    	UserBizRights userBizRights = userBizRightsService.fetch(id);
//    	model.addAttribute("userBizRights", userBizRights);
//        return "module/userbizrights/edit";
//    }
//
//    @RequestMapping(value="/userbizrights/save")
//    @ResponseBody
//    public String save(HttpServletRequest request,UserBizRights userBizRights){
//		boolean isAddNew = StringUtils.isBlank(userBizRights.getId())?true:false;
//		if(isAddNew)
//		{
//			userBizRights.setId(IdGenerator.createNewId());
//			userBizRightsService.add(userBizRights);
//		}
//		else
//		{
//			userBizRightsService.update(userBizRights);
//		}
//        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
//    }
    
    @RequestMapping(value="/userbizrights/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	userBizRightsService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    protected Map getQueryMap(User user) {
        Map<String, Object> conditions = new HashMap();

        conditions.put("userName", user.getUserName());
        conditions.put("realName", user.getRealName());
        conditions.put("password", user.getPassword());
        conditions.put("mobile", user.getMobile());
        conditions.put("email", user.getEmail());
        conditions.put("depId", user.getDepId());
        return conditions;
    }

//    protected Map getQueryMap(UserBizRights userBizRights)
//    {
//    	Map<String, Object> conditions = new HashMap();
//
//		conditions.put("userId", userBizRights.getUserId());
//		conditions.put("userName", userBizRights.getUserName());
//		conditions.put("realName", userBizRights.getRealName());
//		conditions.put("bizUnitId", userBizRights.getBizUnitId());
//		conditions.put("bizUnit", userBizRights.getBizUnit());
//		conditions.put("bizUnitName", userBizRights.getBizUnitName());
//		conditions.put("bizTypeId", userBizRights.getBizTypeId());
//		conditions.put("bizType", userBizRights.getBizType());
//		conditions.put("bizTypeName", userBizRights.getBizTypeName());
//		conditions.put("mailAble", userBizRights.getMailAble());
//		conditions.put("dbaFlag", userBizRights.getDbaFlag());
//		conditions.put("createDate", userBizRights.getCreateDate());
//		conditions.put("createrName", userBizRights.getCreaterName());
//		conditions.put("createrId", userBizRights.getCreaterId());
//    	return conditions;
//    }

//    @Override
//    protected IUserBizRightsService getMainService() {
//        return userBizRightsService;
//    }

    @Override
    protected IUserService getMainService() {
        return userService;
    }

    @Override
    protected String getMainObjName() {
        return "userbizrights";
    }

    @Override
    protected String getViewTitle() {
        return "userbizrights";
    }
}
